package com.example.demo.Service.Impl;

import com.example.demo.Dao.IndexConfigMapper;
import com.example.demo.Dao.ProductMapper;
import com.example.demo.Entity.IndexConfig;
import com.example.demo.Entity.Product;
import com.example.demo.Service.IndexConfigService;
import com.example.demo.Util.BeanUtil;
import com.example.demo.VO.IndexConfigProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class IndexConfigServiceImpl implements IndexConfigService {

    @Autowired
    private IndexConfigMapper indexConfigMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<IndexConfigProductVO> getConfigProductsForIndex(int configType, int number) {
        List<IndexConfigProductVO> indexConfigProductVOS = new ArrayList<>(number);
        /* List<IndexConfig> indexConfigs = indexConfigMapper.findIndexConfigsByTypeAndNum(configType, number);
        if (!CollectionUtils.isEmpty(indexConfigs)) {
            //取出所有的productId
            List<Long> productIds = indexConfigs.stream().map(IndexConfig::getProductId).collect(Collectors.toList());
            List<Product> products = productMapper.selectByPrimaryKeys(productIds);
            indexConfigProductVOS = BeanUtil.copyList(products, IndexConfigProductVO.class);
            for (IndexConfigProductVO indexConfigProductVO : indexConfigProductVOS) {
                String productName = indexConfigProductVO.getProductName();
                String productIntro = indexConfigProductVO.getProductIntro();
                // 字符串过长导致文字超出的问题
                if (productName.length() > 30) {
                    productName = productName.substring(0, 30) + "...";
                    indexConfigProductVO.setProductName(productName);
                }
                if (productIntro.length() > 22) {
                    productIntro = productIntro.substring(0, 22) + "...";
                    indexConfigProductVO.setProductIntro(productIntro);
                }
            }
        }*/
        return indexConfigProductVOS;
    }

    @Override
    public List<IndexConfigProductVO> getAllProducts() {
        List<IndexConfigProductVO> indexConfigProductVOS = new ArrayList<>();
        List<Product> products = productMapper.selectAll();
        indexConfigProductVOS = BeanUtil.copyList(products, IndexConfigProductVO.class);
        for (IndexConfigProductVO indexConfigProductVO : indexConfigProductVOS) {
            String productName = indexConfigProductVO.getProductname();
            String productIntro = indexConfigProductVO.getProductinfo();
            // 字符串过长导致文字超出的问题
            if (productName.length() > 30) {
                productName = productName.substring(0, 30) + "...";
                indexConfigProductVO.setProductname(productName);
            }
            if (productIntro.length() > 22) {
                productIntro = productIntro.substring(0, 22) + "...";
                indexConfigProductVO.setProductinfo(productIntro);
            }
        }
        return indexConfigProductVOS;
    }
}
